mui.init({
	swipeBack:false, //启用右滑关闭功能
	keyEventBind: {
		backbutton: false  //关闭back按键监听
	}
}); 

if(window.plus){
    plusReady();
}else{
    document.addEventListener('plusready',plusReady,false);
}
function plusReady(){
	$("body").height(plus.display.resolutionHeight);
    // 读取用户密码
    writeRemberMessage();
}
        
//登录事件
var btnenable="true";
var first = null;
var login_click=function (){
	panUtil.setCloseTime(first,3000,'您点的太快了啊，休息下再点吧。',loginajax);
}
//写入信息
function writeRemberMessage (){
	document.getElementById("username").value=plus.storage.getItem("username");
	//自动登录
	if(plus.storage.getItem("remember")=='true'){
	    document.getElementById("password").value=plus.storage.getItem("password");
	}
}

var loginajax=function(){
	var username=eval(document.getElementById('username')).value;
    var password=eval(document.getElementById('password')).value;
    
    plus.storage.setItem("username",username);
    plus.storage.setItem("remember",document.getElementById("remember").classList.contains('mui-active')?'true':'false');
    if(document.getElementById("remember").classList.contains('mui-active')){
    	plus.storage.setItem("password",password);
    }
    if(btnenable=="true"){
       btnenable="false";
       panUtil.ajaxLoding.start();
       mui.ajax(baseServerAdrr+'/login/logincheck',{
	      data:{
		   username:username,
		   password:password,
		   deviceid:plus.device.uuid
	      },
	      dataType:'json',//服务器返回json格式数据
	      type:'post',//HTTP请求类型
	      timeout:10000,//超时时间设置为10秒；
	      success:function(data){
	      	panUtil.ajaxLoding.stop();//隐藏等待
	      	btnenable="true";//使按钮不能点击
	      	//判断返回的是否为:binddevice--如果是的话则进行绑定手机
	      	if(data.id=='binddevice'){
	      		plus.nativeUI.confirm(data.message,function(e){
		    		if(e.index==0) {
		    			//用户与手机绑定
		    			bindMobile(username,password, plus.device.uuid);
		    		}
		    	},'温馨提示',["是","否"]);
	      	}else if(data.id=='hasbinded'){	
	      		plus.nativeUI.alert(data.message,function(){},'温馨提示',"确定");
	      	}else if(data.id=='success'){
		   	     plus.storage.setItem("token",data.token);
		   	     if(data.userinfo!=null){
		   	     	plus.storage.setItem("usercaption",data.userinfo.t_username);
		   	     }else{
		   	     	plus.storage.setItem("usercaption",username);
		   	     }
		   	     //打开主页面
                 mui.openWindow({url: 'main.html',id:'mainpage',createNew:true,waiting:{autoShow:false}});//
		    }else{
		   	    mui.toast(data.message);
		   	    setTimeout(function(){
		   	    	panUtil.ReLogin();
		   	    },1000);
		    }
		    
	      },
	      error:function(xhr,type,errorThrown){
	      	  panUtil.ajaxLoding.stop();
		      //异常处理；
		      btnenable="true";
		      mui.toast('服务器异常登录失败!');
	      }
       });
   }else{
   	  mui.toast('您点的太快了啊，休息下再点吧。');
   }
}
//用户与手机绑定
function bindMobile(username,password, uuid) {
	panUtil.ajaxLoding.start('绑定进行中...');
    mui.ajax(baseServerAdrr+'/login/bindmobile',{
	    data: {
	   		username:username,
	   		deviceid:uuid,
	   		password:password
	    },
	    dataType:'json',//服务器返回json格式数据
	    type:'post',//HTTP请求类型
	    timeout:30000,//超时时间设置为10秒；
	    success:function(data){
		    panUtil.ajaxLoding.stop();
		    mui.toast(data.message);
		    if(data.id=="success"){
		    	loginajax();
		    }
	    },
	    error:function(xhr,type,errorThrown){
		    //异常处理；
		    panUtil.ajaxLoding.stop();
		    mui.toast('服务器异常绑定失败!');
	    }
	});
}